package br.com.uniti.academico.persistence.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public abstract class HashUtils {

	private static MessageDigest sha1;

	static {
		try {
			sha1 = MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
	}

	private static String toHexString(final byte[] digest) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < digest.length; i++) {
			String s = Integer.toHexString(0xFF & digest[i]);
			if (s.length() == 1) {
				sb.append('0');
			}
			sb.append(s);
		}
		return sb.toString();
	}

	public static String createSHA1(final byte[] bytes) {
		sha1.reset();
		sha1.update(bytes);
		byte[] digest = sha1.digest();

		return toHexString(digest);
	}

	public static String createSHA1(final String input) {
		return input == null ? null : createSHA1(input.getBytes());
	}

}
